package com.lh.music.fragment.localsong;


import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.lh.music.R;
import com.lh.music.activity.SongDetailActivity;
import com.lh.music.adapter.SingerAdapter;
import com.lh.music.datamanager.DataManager;
import com.lh.music.entity.Singer;
import com.lh.music.entity.Song;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class SingerFragment extends Fragment {
    private View view;
    private ListView listView;
    private SingerAdapter adapter;
    private List<Singer> mData=new ArrayList<>();

    public SingerFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(final LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        if (view==null){
            view=inflater.inflate(R.layout.fragment_singer, container, false);
            listView= (ListView) view.findViewById(R.id.listView);
            adapter=new SingerAdapter(getActivity(),mData);
            listView.setAdapter(adapter);
            new MyTask().execute();
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent intent=new Intent(getActivity(), SongDetailActivity.class);
                    Bundle bundle=new Bundle();
                    ArrayList<Song> l= (ArrayList<Song>) mData.get(position).getSongList();
                    bundle.putSerializable("songList",l);
                    intent.putExtra("bundle",bundle);
                    intent.putExtra("flag","singer");
                    startActivity(intent);
                }
            });
        }
        return view;
    }

    class MyTask extends AsyncTask<Void,Void,Void> {


        @Override
        protected Void doInBackground(Void... params) {
            DataManager dataManager=new DataManager(getActivity());
            List<Song> songs=dataManager.findAllSongOrderBySinger();
            List<Song> songList=null;
            Singer singer=null;
            for(int i=0;i<songs.size();i++){
                //如果为0  新建一个歌手
                if (i==0){
                    singer=new Singer();
                    songList=new ArrayList<>();
                    //初始化name
                    singer.setName(songs.get(i).getArtist());
                    singer.setPicUrl(songs.get(i).getAlbum_img());
                    //将该首歌添加到该歌手的歌单中
                    songList.add(songs.get(i));
                    //只有一首歌的情况
                    if (i== songs.size()-1){
                        singer.setSongList(songList);
                        mData.add(singer);
                    }
                }
                //如果当歌曲的歌手和上一首歌是同一位，将这首歌添加到songlist中
                else if(songs.get(i).getArtist().equals(songs.get(i-1).getArtist())){
                    songList.add(songs.get(i));
                    //如果到头了
                    if (i==songs.size()-1){
                        singer.setSongList(songList);
                        mData.add(singer);
                    }
                }else{
                    //如果不一样，先为上一位歌手，绑定歌单
                    singer.setSongList(songList);
                    mData.add(singer);
                    //新建歌手
                    singer=new Singer();
                    songList=new ArrayList<>();
                    singer.setName(songs.get(i).getArtist());
                    singer.setPicUrl(songs.get(i).getAlbum_img());
                    songList.add(songs.get(i));
                    //如果到头了，直接加进去
                    if (i==songs.size()-1){
                        singer.setSongList(songList);
                        mData.add(singer);
                    }
                }
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void list) {
            adapter.notifyDataSetChanged();
        }
    }

}
